package com.yexuejc.docker.image;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Service {
    /**
     * 执行命令
     *
     * @param exec
     * @param printWriter
     */
    public void exec(String[] exec, PrintWriter printWriter) {
        InputStream in = null;
        try {
            Process pro = Runtime.getRuntime().exec(exec);
            int status = pro.waitFor();
            if (status != 0) {
                System.err.println("Failed to call shell's command and the return status's is: " + status);
            }
            in = pro.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String result = "";
            while ((result = read.readLine()) != null) {
                System.out.println("INFO:" + result);
                printWriter.write("INFO:" + result + "\n");
            }
            read.close();
        } catch (Exception e) {
            e.printStackTrace();
            printWriter.write("ERR:" +e.getMessage());
        }
    }
}
